<template>
  <div class="nr">
        <el-table :data="list" class="table">
          <el-table-column prop="name" label="账号" fixed="left" width="150px"></el-table-column>
          <el-table-column prop="password" label="密码" width="300px"></el-table-column>
          <el-table-column label="招标相关权限" width="150px">
            <template slot-scope="scope">
              <el-switch :value="scope.row.admin1" :active-value="1" :inactive-value="defaultValue" @change="changeAdmin(scope.row, 1)" active-color="red"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="举报相关权限" width="150px">
            <template slot-scope="scope">
              <el-switch :value="scope.row.admin2" :active-value="1" :inactive-value="defaultValue" @change="changeAdmin(scope.row, 2)" active-color="red"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="高级管理员" width="150px">
            <template slot-scope="scope">
              <el-switch :value="scope.row.admin3" :active-value="1" :inactive-value="defaultValue" @change="changeAdmin(scope.row, 3)" active-color="red"></el-switch>
            </template>
          </el-table-column>
          <el-table-column width="150px">
            <template slot-scope="scope">
              <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination background layout="prev, pager, next" @current-change="changePage" :current-page="1" :page-count="page"></el-pagination>
  </div>
</template>

<script>
import { Notification } from 'element-ui'
import { getAll, getSysUser, deleteById, updateAdmin } from '@/api/user'
export default {
  name: 'AuthorityIndex',
  data () {
    return {
      page: 0,
      listPage: [],
      list: [],
      defaultValue: 0,
      admin1: -1,
      admin2: -1,
      admin3: -1
    }
  },
  computed: {
    name () {
      return this.$store.getters.name
    }
  },
  async created () {
    const res = await getSysUser(this.name)
    this.user = res.data
    if (this.user.admin3 !== 1) {
      this.$message({
        message: '暂无该权限',
        type: 'error'
      })
      this.$store.commit('zbUn/setSearchInfo', { search: '', show: 1 })
      this.$router.go()
    }
    const res1 = await getAll()
    if (!res1.isSuccess) {
      return null
    } else {
      this.list = res1.data
      this.page = this.list.length / 10
      this.listPage = this.list.slice(0, 10)
    }
  },
  methods: {
    async changeAdmin (row, n) {
      if (n === 1) {
        if (row.admin1 === 0) {
          this.admin1 = 1
        } else {
          this.admin1 = 0
        }
      }
      if (n === 2) {
        if (row.admin2 === 0) {
          this.admin2 = 1
        } else {
          this.admin2 = 0
        }
      }
      if (n === 3) {
        if (row.admin3 === 0) {
          this.admin3 = 1
        } else {
          this.admin3 = 0
        }
      }
      const res = await updateAdmin(row.name, this.admin1, this.admin2, this.admin3)
      if (res.isSuccess) {
        Notification({
          title: res.message,
          type: 'success'
        })
      } else {
        Notification({
          title: res.message,
          type: 'error'
        })
      }
      this.$store.commit('zbUn/setSearchInfo', { search: '', show: 7 })
      this.$router.go()
    },
    async del (id) {
      const res = await deleteById(id)
      Notification({
        title: res.message,
        type: 'success'
      })
      this.$store.commit('zbUn/setSearchInfo', { search: '', show: 7 })
      this.$router.go()
    },
    changePage (page) {
      if (page === this.page) {
        const n = this.list % 10
        this.listPage = this.list.slice(10 * (page - 1), 10 * (page - 1) + n)
      }
      this.listPage = this.list.slice(10 * (page - 1), 10 * (page - 1) + 10)
    }
  }
}
</script>

<style lang="less" scoped>
  .nr {
    width: 100%;
    border-top: 1px solid;
    .table {
      width: 80%;
      margin-left: 10%;
      margin-bottom: 2%;
    }
  }
</style>
